package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.OvertimeApply;
import com.amsp.oa.oademo.service.OvertimeApplyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/overtime-apply")
@Api(tags = "加班申请接口")
public class OvertimeApplyController {

    @Autowired
    private OvertimeApplyService overtimeApplyService;

    @PostMapping("/save")
    @ApiOperation("保存加班申请")
    public ResponseEntity<List<OvertimeApply>> save(@RequestBody Map<String, Object> params) {
        List<Map<String, Object>> overtimeRecords = (List<Map<String, Object>>) params.get("overtimeRecords");
        String applicationDate = (String) params.get("applicationDate");
        String applicant = (String) params.get("applicant");
        String reviewer = (String) params.get("reviewer");
        String approver = (String) params.get("approver");

        if (!overtimeRecords.isEmpty()) {
            List<OvertimeApply> applies = new ArrayList<>();

            for (Map<String, Object> record : overtimeRecords) {
                OvertimeApply apply;
                Object idObj = record.get("id");
                if (idObj != null) {
                    Long id = Long.valueOf(idObj.toString());
                    apply = overtimeApplyService.getById(id);
                    if (apply == null) {
                        apply = new OvertimeApply();
                    }
                } else {
                    apply = new OvertimeApply();
                }

                apply.setEmployeeId((String) record.get("employeeId"));
                apply.setName((String) record.get("name"));
                apply.setDepartment((String) record.get("department"));
                apply.setOvertimeDate(LocalDate.parse((String) record.get("overtimeDate")));
                apply.setOvertimePeriod((String) record.get("overtimePeriod"));
                apply.setTotalHours(Double.valueOf(record.get("totalHours").toString()));
                apply.setReason((String) record.get("reason"));

                apply.setApplicationDate(LocalDate.parse(applicationDate));
                apply.setApplicant(applicant);
                apply.setReviewer(reviewer);
                apply.setApprover(approver);

                if (apply.getCreateTime() == null) {
                    apply.setCreateTime(LocalDateTime.now());
                }
                apply.setUpdateTime(LocalDateTime.now());

                applies.add(apply);
            }

            overtimeApplyService.saveOrUpdateBatch(applies);
            return ResponseEntity.ok(applies);
        }
        return ResponseEntity.badRequest().build();
    }

    @GetMapping("/list")
    @ApiOperation("获取加班申请列表")
    public ResponseEntity<IPage<OvertimeApply>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String employeeId,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String department) {
        return ResponseEntity.ok(overtimeApplyService.getPage(current, size, employeeId, name, department));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取加班申请详情")
    public ResponseEntity<OvertimeApply> getById(@PathVariable Long id) {
        return ResponseEntity.ok(overtimeApplyService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除加班申请")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        overtimeApplyService.removeById(id);
        return ResponseEntity.ok().build();
    }
}